def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

def max_prime(num):
    if len(str(num)) != 4:
        return 0
    digits = [int(d) for d in str(num)]
    max_prime_num = 0
    for i in range(4):
        for j in range(4):
            for k in range(4):
                for l in range(4):
                    if i != j and i != k and i != l and j != k and j != l and k != l:
                        current_num = digits[i] * 1000 + digits[j] * 100 + digits[k] * 10 + digits[l]
                        if is_prime(current_num) and current_num > max_prime_num:
                            max_prime_num = current_num
    return max_prime_num

num = int(input("Enter a four-digit natural number: "))
result = max_prime(num)
print(result)
